package com.plugin.storage.storage.model;

import com.google.common.base.Strings;
import com.plugin.storage.base.SdkClient;
import com.plugin.storage.base.SdkContext;
import com.plugin.storage.storage.StorageUtils;
import com.plugin.storage.storage.spi.VideoProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

import static com.google.common.io.Files.getNameWithoutExtension;

public class UploadVideoReq extends SdkClient {

    private static final Logger LOGGER = LoggerFactory.getLogger(UploadVideoReq.class);

    /**
     * 本地文件路径
     */
    private String localPath;

    /**
     * 本地文件
     */
    private File localFile;

    /**
     * springmvc 上传文件
     */
    private MultipartFile multipartFile;

    /**
     * 文件名
     */
    private String fileName;

    private String title;

    private String description;

    private Integer cateId;

    private String tags;

    private String templateGroupId;

    private String coverUrl;

    public UploadVideoReq() {
    }

    public UploadVideoReq(File file) {
        this.localFile = file;
    }

    public UploadVideoReq(MultipartFile multipartFile) {
        this.multipartFile = multipartFile;
    }

    public File getLocalFile() {
        return localFile;
    }

    public UploadVideoReq setLocalFile(File localFile) {
        this.localFile = localFile;
        return this;
    }

    public MultipartFile getMultipartFile() {
        return multipartFile;
    }

    public UploadVideoReq setMultipartFile(MultipartFile multipartFile) {
        this.multipartFile = multipartFile;
        return this;
    }

    public String getLocalPath() {
        return localPath;
    }

    public UploadVideoReq setLocalPath(String localPath) {
        this.localPath = localPath;
        return this;
    }

    public String getFileName() {
        return fileName;
    }

    public UploadVideoReq setFileName(String fileName) {
        this.fileName = fileName;
        return this;
    }

    public String getTitle() {
        return title;
    }

    public UploadVideoReq setTitle(String title) {
        this.title = title;
        return this;
    }

    public String getCoverUrl() {
        return coverUrl;
    }

    public UploadVideoReq setCoverUrl(String coverUrl) {
        this.coverUrl = coverUrl;
        return this;
    }

    public String getDescription() {
        return description;
    }

    public UploadVideoReq setDescription(String description) {
        this.description = description;
        return this;
    }

    public Integer getCateId() {
        return cateId;
    }

    public UploadVideoReq setCateId(Integer cateId) {
        this.cateId = cateId;
        return this;
    }

    public String getTags() {
        return tags;
    }

    public UploadVideoReq setTags(String tags) {
        this.tags = tags;
        return this;
    }

    public String getTemplateGroupId() {
        return templateGroupId;
    }

    public UploadVideoReq setTemplateGroupId(String templateGroupId) {
        this.templateGroupId = templateGroupId;
        return this;
    }

    public UploadVideo.Auth auth() {
        VideoProvider videoProvider = getVideoProvider();
        SdkContext sdkContext = getContext();
        if (Strings.isNullOrEmpty(this.fileName)) {
            setFileName(StorageUtils.getFileName());
        }
        if (Strings.isNullOrEmpty(title)) {
            title = getNameWithoutExtension(fileName);
        }
        return videoProvider.auth(sdkContext, this);
    }

    public UploadVideo.Auth auth(String fileName) {
        setFileName(fileName);
        return auth();
    }

    public UploadVideo upload() {
        VideoProvider videoProvider = getVideoProvider();
        SdkContext sdkContext = getContext();
        if (localFile != null) {
            localPath = localFile.getAbsolutePath();
            fileName = localFile.getName();
        }
        if (multipartFile != null) {
            try {
                fileName = multipartFile.getOriginalFilename();
                File tempFile = File.createTempFile(multipartFile.getOriginalFilename(), ".tmp");
                tempFile.deleteOnExit();
                multipartFile.transferTo(tempFile);
                localPath = tempFile.getAbsolutePath();
            } catch (IOException e) {
                LOGGER.error(e.getMessage(), e);
            }
        }
        if (Strings.isNullOrEmpty(title)) {
            title = getNameWithoutExtension(fileName);
        }
        return videoProvider.upload(sdkContext, this);
    }

}
